package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAppointmentRecordMapper;
import com.ruoyi.system.domain.SysAppointmentRecord;
import com.ruoyi.system.service.ISysAppointmentRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 预约记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-10
 */
@Service
public class SysAppointmentRecordServiceImpl implements ISysAppointmentRecordService
{
    @Autowired
    private SysAppointmentRecordMapper sysAppointmentRecordMapper;

    /**
     * 查询预约记录
     *
     * @param id 预约记录主键
     * @return 预约记录
     */
    @Override
    public SysAppointmentRecord selectSysAppointmentRecordById(Long id)
    {
        return sysAppointmentRecordMapper.selectSysAppointmentRecordById(id);
    }

    /**
     * 根据客户主键查询预约记录
     * @param customerId
     * @return
     */
    @Override
    public List<SysAppointmentRecord> selectSysAppointmentRecordByCustomerId(Long customerId) {
        return sysAppointmentRecordMapper.selectSysAppointmentRecordByCustomerId(customerId);
    }

    /**
     * 查询预约记录列表
     *
     * @param sysAppointmentRecord 预约记录
     * @return 预约记录
     */
    @Override
    public List<SysAppointmentRecord> selectSysAppointmentRecordList(SysAppointmentRecord sysAppointmentRecord)
    {
        return sysAppointmentRecordMapper.selectSysAppointmentRecordList(sysAppointmentRecord);
    }

    /**
     * 新增预约记录
     *
     * @param sysAppointmentRecord 预约记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysAppointmentRecord(SysAppointmentRecord sysAppointmentRecord)
    {
        int status = 0;
        try {
            //新增预约记录判断方法
            emptyInsertSysAppointmentRecord(sysAppointmentRecord);

            //创建人
            sysAppointmentRecord.setCreateBy(SecurityUtils.getUsername());

            //创建时间
            sysAppointmentRecord.setCreateTime(DateUtils.getNowDate());

            //新增预约状态
            sysAppointmentRecord.setIsVisit(0L);

            //新增方法
            status = sysAppointmentRecordMapper.insertSysAppointmentRecord(sysAppointmentRecord);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 确认上门
     * @param param 预约记录主键
     * @return
     */
    @Override
    public int approvAppointmentRecord(JSONObject param) {
        int status = 0;
        try {
            //查询预约记录是否存在
            SysAppointmentRecord sysAppointmentRecord = this.selectSysAppointmentRecordById(param.getLong("id"));
            if (null == sysAppointmentRecord){
                throw new RuntimeException("预约记录不存在请联系管理员");
            }
            //判断预约记录是否为已上门
            if (sysAppointmentRecord.getIsVisit() == 1){
                throw new RuntimeException("已上门不可重复确认");
            }
            //确认人
            sysAppointmentRecord.setApproverName(SecurityUtils.getUsername());
            //确认时间
            sysAppointmentRecord.setApproverTime(DateUtils.getNowDate());
            //预约备注
            if(StringUtils.isNotEmpty(param.getString("approverNote"))){
                sysAppointmentRecord.setApproverNote(param.getString("approverNote"));
            }
            //预约上门状态
            sysAppointmentRecord.setIsVisit(1L);
            //修改预约记录
            status = this.updateSysAppointmentRecord(sysAppointmentRecord);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 新增预约记录判断方法
     * @param sysAppointmentRecord
     */
    @Override
    public void emptyInsertSysAppointmentRecord(SysAppointmentRecord sysAppointmentRecord) {
        //线索客户主键
        if (null == sysAppointmentRecord.getCustomerId()){
            throw new RuntimeException("线索客户不可为空");
        }
        //预约时间
        if (null == sysAppointmentRecord.getAppointmentTime()){
            throw new RuntimeException("预约时间不可为空");
        }
        //访谈时间
        if (null == sysAppointmentRecord.getInterviewTime()){
            throw new RuntimeException("访谈时间不可为空");
        }
        //签约可能
        if (null == sysAppointmentRecord.getIsSign()){
            throw new RuntimeException("签约可能必须选择");
        }
    }

    /**
     * 修改预约记录
     *
     * @param sysAppointmentRecord 预约记录
     * @return 结果
     */
    @Override
    public int updateSysAppointmentRecord(SysAppointmentRecord sysAppointmentRecord)
    {
        sysAppointmentRecord.setUpdateTime(DateUtils.getNowDate());
        return sysAppointmentRecordMapper.updateSysAppointmentRecord(sysAppointmentRecord);
    }

    /**
     * 批量删除预约记录
     *
     * @param ids 需要删除的预约记录主键
     * @return 结果
     */
    @Override
    public int deleteSysAppointmentRecordByIds(Long[] ids)
    {
        return sysAppointmentRecordMapper.deleteSysAppointmentRecordByIds(ids);
    }

    /**
     * 删除预约记录信息
     *
     * @param id 预约记录主键
     * @return 结果
     */
    @Override
    public int deleteSysAppointmentRecordById(Long id)
    {
        return sysAppointmentRecordMapper.deleteSysAppointmentRecordById(id);
    }
}